package chapter14.nullperson;

import java.util.ArrayList;

/**
 * @author : DuJiabao
 * @Date : 2021/2/25 16:41
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class Staff extends ArrayList<Position> {
  public Staff() {
  }

  public Staff(String... titles) {
    add(titles);
  }

  public void add(String title, Person person) {
    add(new Position(title, person));
  }

  public void add(String... titles) {
    for (String title : titles) {
      add(new Position(title));
    }
  }

  public boolean positionAvailable(String title) {
    for (Position position : this) {
      if (position.getTitle().equals(title) && position.getPerson().equals(Person.NULL))
        return true;
    }
    return false;
  }

  public void fillPosition(String title, Person hire) {
    for (Position position : this) {
      if (position.getTitle().equals(title) && position.getPerson().equals(Person.NULL)) {
        position.setPerson(hire);
        return;
      }
    }
    throw new RuntimeException("Position " + title + "not available");
  }

  public static void main(String[] args) {
    Staff staff = new Staff(
            "President", "CTO", "Marketing Manager", "Product Manager", "Project Lead",
            "Software Engineer", "Software Engineer", "Software Engineer", "Software Engineer",
            "Test Engineer", "Technical Writer"
    );
    staff.fillPosition("President", new Person("Me", "Last", "The Top, Lonely At"));
    System.out.println(staff);
  }
}
